package com.cssw.fyzb.busi.service.acct;

import com.cssw.fyzb.core.dao.acct.*;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.acct.AcctGlobalVo;
import com.cssw.fyzb.core.model.vo.acct.SoundValueVo;
import com.cssw.fyzb.busi.util.BizUtil;
import com.cssw.fyzb.busi.util.UuidUtil;
import com.cssw.fyzb.core.dao.system.SysParamDao;
import com.cssw.fyzb.core.eao.acct.AcctLogEao;
import com.cssw.fyzb.core.model.eos.AcctLogEo;
import com.cssw.fyzb.core.model.vo.acct.UserAcctVo;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author zhouxu
 * @create 2020-12-18 09:46
 */
@Repository
public class AcctServiceImpl implements AcctService {

    private static final Logger logger = LoggerFactory.getLogger(AcctServiceImpl.class);

    @Autowired
    private AcctDao acctDao;
    @Autowired
    private AcctTransferDao acctTransferDao;
    @Autowired
    private AcctExtDao acctExtDao;
    @Autowired
    private AcctLogEao acctLogEao;
    @Autowired
    private SysParamDao sysParamDao;

    @Autowired
    private TeamGzPriceDao teamGzPriceDao;

    @Autowired
    private RnbExchangeCoinLogDao rnbExchangeCoinLogDao;

    @Override
    public Long createAcct(Long userId, String acctType) {
        Acct acct = acctDao.queryAcctByUIdAndType(userId, acctType);
        if (null != acct) {
            return acct.getAcctId();
        }
        return acctDao.createAcct(userId, acctType);
    }

    @Override
    public Acct queryAcctByUIdAndType(Long userId, String acctType) {
        return acctDao.queryAcctByUIdAndType(userId, acctType);
    }

    @Override
    public AcctGlobalVo queryAcctGlobal(Long userId) {
        List<Acct> acctList = acctDao.listAcctByUserId(userId);

        AcctGlobalVo acctGlobalVo = new AcctGlobalVo();
        for (Acct acct : acctList) {
            if (acct.isF()) {
                acctGlobalVo.setFBalance(acct.getBalanceFee().setScale(2,BigDecimal.ROUND_HALF_UP));
            } else if (acct.isY()) {
                acctGlobalVo.setYBalance(acct.getBalanceFee().setScale(2,BigDecimal.ROUND_HALF_UP));
            } else if (acct.isG()) {
                acctGlobalVo.setGBalance(acct.getBalanceFee().setScale(7,BigDecimal.ROUND_HALF_UP));
            }
        }

        return acctGlobalVo;
    }

    @Override
    public Page<AcctLogEo> queryAcctLog(Long userId, String acctType, Integer page, Integer limit) {
        Acct acct = acctDao.queryAcctByUIdAndType(userId, acctType);
        if (acct == null) {
            BizUtil.exception("用户账本异常");
        }
        return acctLogEao.pageByType(page, limit, userId, acctType);


    }

    @Override
    public void addBalance(Long userId, String acctType, BigDecimal value, String tradeId, String tradeType, String busiType, String tradeName) {
        acctDao.addBalance(userId, acctType, value, tradeId, tradeType, busiType);
        Acct acct = acctDao.queryAcctByUIdAndType(userId, acctType);
        //增加ES日志
        acctLogEao.save(UuidUtil.get(), acct.getAcctId(), userId, acctType, value,
                Acct.FEE_IN_DE_ADD, tradeId.toString(), tradeType, tradeName,
                busiType, new Date());
    }

    @Override
    public void minusBalance(Long userId, String acctType, BigDecimal value, String tradeId, String tradeType, String busiType, String tradeName) {
        acctDao.minusBalance(userId, acctType, value, tradeId, tradeType, busiType);
        Acct acct = acctDao.queryAcctByUIdAndType(userId, acctType);
        //增加ES日志
        acctLogEao.save(UuidUtil.get(), acct.getAcctId(), userId, acctType, value,
                Acct.FEE_IN_DE_MINUS, tradeId.toString(), tradeType, tradeName,
                busiType, new Date());
    }

    @Override
    public List<RnbExchangeFDef> queryRnbBuyFDef() {
        List<RnbExchangeFDef> list = acctExtDao.listRnbExchangeFDef();
        return list;
    }

    @Override
    public List<RnbExchangeGzDef> queryRnbBuyGzDef() {
        List<RnbExchangeGzDef> list = acctExtDao.listRnbExchangeGzDef();
        return list;
    }

    @Override
    public RnbExchangeFDef findRnbBuyFDef(Long rfId) {
        return acctExtDao.findRnbExchangeFDefById(rfId);
    }

    @Override
    public RnbExchangeGzDef findRnbBuyGzDef(Long rgId) {
        return acctExtDao.findRnbExchangeGzDefById(rgId);
    }

    @Override
    public void transfer(String transferType, BigDecimal money, Long fromUserId, Long toUserId) {

    }

    //添加转账日志
    @Override
    public Long addTransferLog(String acctType, Long fromUserId, Long toUserId, BigDecimal amount, String remark) {
        return acctTransferDao.addLog(acctType, fromUserId, toUserId, amount, remark);
    }

    @Override
    public SoundValueVo soundValue(Long userId) {
        //去兑换比例
        SoundValueVo soundValueVo = new SoundValueVo();
        SysParam sysParam = sysParamDao.findByCode(SysParam.Y_EXCHANGE_R_RATIO);
        soundValueVo.setValue(sysParam.getParamValue());
        //用户当前音值
        Acct acct = acctDao.queryAcctByUIdAndType(userId, AcctType.ACCT_TYPE_Y);
        soundValueVo.setUserY(acct.getBalanceFee());
        return soundValueVo;
    }

    @Override
    public UserAcctVo cmfUser(Long userId, String acctType) {
        UserAcctVo userAcctVo = new UserAcctVo();
        CmfUser cmfUser = CmfUser.dao.findById(userId);
        Acct acct = acctDao.queryAcctByUIdAndType(userId, acctType);
        userAcctVo.setUserId(userId);
//        userAcctVo.setCoin(cmfUser.getCoin());
//        userAcctVo.setYzCoin(cmfUser.getYzCoin());

        return userAcctVo;
    }

    @Override
    public TeamGzPrice getTeamGzPriceByNum(Integer num) {
        return teamGzPriceDao.getByNum(num);
    }

    @Override
    public List<TeamGzPrice> listTeamGzPrice() {
        return teamGzPriceDao.list();
    }

    @Override
    public void saveRnbExchangeCoinLog(Long userId, BigDecimal rnb, String acctType, BigDecimal coin,
                                       String tradeNo, String status, String remark) {
        rnbExchangeCoinLogDao.save(userId, rnb, acctType, coin, tradeNo, status, remark);
    }
}
